package com.habitreformer.core.habit;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import junit.framework.TestCase;

import com.habitreformer.core.task.Task;
import com.habitreformer.core.task.TaskRating;

public class HabitImplTest extends TestCase
{
	HabitImpl habit;

	protected void setUp() throws Exception
	{
		habit = new HabitImpl("testing");
	}

	public void testHabitImpl()
	{
		habit = new HabitImpl("test");
		assertEquals(habit.getName(), "test");
		assertEquals(0, habit.getCurrentIndex());
		Date date = habit.getStartDate();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		assertEquals(Calendar.JANUARY, calendar.get(Calendar.MONTH));
		assertEquals(1, calendar.get(Calendar.DATE));
		assertEquals(2010, calendar.get(Calendar.YEAR));
		assertEquals(HabitStatus.PROCEEDING, habit.getStatus());
	}

	public void testGetCurrentIndex()
	{
		habit = new HabitImpl("test");
		assertEquals(0, habit.getCurrentIndex());
		habit.finishCurrentTask(TaskRating.EXCELLENT);
		assertEquals(1, habit.getCurrentIndex());
	}

	public void testSetName()
	{
		habit = new HabitImpl("name");
		assertEquals("name", habit.getName());
		habit.setName("another");
		assertEquals("another", habit.getName());

		habit.setName(null);
		assertNull(habit.getName());

	}

	public void testIsCompleted()
	{
		assertFalse(habit.isCompleted());
	}

	public void testGetName()
	{
		habit = new HabitImpl("test");
		assertEquals("test", habit.getName());

		habit = new HabitImpl(null);
		assertNull(habit.getName());
	}

	public void testGetStartDate()
	{
		Date date = habit.getStartDate();
		assertNotNull(date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		assertEquals(Calendar.JANUARY, calendar.get(Calendar.MONTH));
		assertEquals(1, calendar.get(Calendar.DATE));
		assertEquals(2010, calendar.get(Calendar.YEAR));
	}

	public void testGetTaskList()
	{
		List<Task> list = habit.getTaskList();
		assertEquals(21, list.size());
	}

	public void testGetStatus()
	{
		assertEquals(HabitStatus.PROCEEDING, habit.getStatus());
	}

	public void testFinishCurrentTask()
	{
		int length = habit.getTaskList().size();
		for (int i = 0; i < length - 1; i++)
		{
			habit.finishCurrentTask(TaskRating.EXCELLENT);
			assertFalse(habit.isCompleted());
		}
		habit.finishCurrentTask(TaskRating.EXCELLENT);
		assertTrue(habit.isCompleted());
	}

	public void testComplete()
	{
		assertFalse(habit.isCompleted());
		habit.complete();
		assertTrue(habit.isCompleted());
	}

}
